package com.agile.major.web.rest;

import com.agile.major.domain.MajorCoursePlan;
import com.agile.major.service.MajorCoursePlanService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing MajorCoursePlan.
 */
@RestController
@RequestMapping("/api")
public class MajorCoursePlanResource {

    private final Logger log = LoggerFactory.getLogger(MajorCoursePlanResource.class);

    private static final String ENTITY_NAME = "majorCoursePlan";

    private final MajorCoursePlanService majorCoursePlanService;

    public MajorCoursePlanResource(MajorCoursePlanService majorCoursePlanService) {
        this.majorCoursePlanService = majorCoursePlanService;
    }

    /**
     * POST  /major-course-plans : Create a new majorCoursePlan.
     *
     * @param majorCoursePlan the majorCoursePlan to create
     * @return the ResponseEntity with status 201 (Created) and with body the new majorCoursePlan, or with status 400 (Bad Request) if the majorCoursePlan has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/major-course-plans")
    @Timed
    public ResponseEntity<MajorCoursePlan> createMajorCoursePlan(@RequestBody MajorCoursePlan majorCoursePlan) throws URISyntaxException {
        log.debug("REST request to save MajorCoursePlan : {}", majorCoursePlan);
        if (majorCoursePlan.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new majorCoursePlan cannot already have an ID")).body(null);
        }
        MajorCoursePlan result = majorCoursePlanService.save(majorCoursePlan);
        return ResponseEntity.created(new URI("/api/major-course-plans/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /major-course-plans : Updates an existing majorCoursePlan.
     *
     * @param majorCoursePlan the majorCoursePlan to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated majorCoursePlan,
     * or with status 400 (Bad Request) if the majorCoursePlan is not valid,
     * or with status 500 (Internal Server Error) if the majorCoursePlan couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/major-course-plans")
    @Timed
    public ResponseEntity<MajorCoursePlan> updateMajorCoursePlan(@RequestBody MajorCoursePlan majorCoursePlan) throws URISyntaxException {
        log.debug("REST request to update MajorCoursePlan : {}", majorCoursePlan);
        if (majorCoursePlan.getId() == null) {
            return createMajorCoursePlan(majorCoursePlan);
        }
        MajorCoursePlan result = majorCoursePlanService.save(majorCoursePlan);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, majorCoursePlan.getId().toString()))
            .body(result);
    }

    /**
     * GET  /major-course-plans : get all the majorCoursePlans.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of majorCoursePlans in body
     */
    @GetMapping("/major-course-plans")
    @Timed
    public ResponseEntity<Page<MajorCoursePlan>> getAllMajorCoursePlans(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of MajorCoursePlans");
        Page<MajorCoursePlan> page = majorCoursePlanService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/major-course-plans");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }
    @GetMapping("/major-course-plans/keyword")
    @Timed
    public ResponseEntity<Page<MajorCoursePlan>> getLikeBaseCourses(@RequestParam("keyword") String keyword,@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of MajorCoursePlan");
        Page<MajorCoursePlan> page = majorCoursePlanService.likeCourses(keyword,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/major-course-plans");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }
    /**
     * @GetMapping("/base-courses/keyword")
     @Timed
     public ResponseEntity<Page<BaseCourse>> getAllBaseCourses(@RequestParam("keyword") String keyword,@ApiParam Pageable pageable) {
     log.debug("REST request to get a page of BaseCourses");
     Page<BaseCourse> page = baseCourseService.findAll(keyword,pageable);
     HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/base-courses");
     return new ResponseEntity<>(page, headers, HttpStatus.OK);
     }
     */

    /**
     * GET  /major-course-plans/:id : get the "id" majorCoursePlan.
     *
     * @param id the id of the majorCoursePlan to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the majorCoursePlan, or with status 404 (Not Found)
     */
    @GetMapping("/major-course-plans/{id}")
    @Timed
    public ResponseEntity<MajorCoursePlan> getMajorCoursePlan(@PathVariable Long id) {
        log.debug("REST request to get MajorCoursePlan : {}", id);
        MajorCoursePlan majorCoursePlan = majorCoursePlanService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(majorCoursePlan));
    }

    /**
     * DELETE  /major-course-plans/:id : delete the "id" majorCoursePlan.
     *
     * @param id the id of the majorCoursePlan to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/major-course-plans/{id}")
    @Timed
    public ResponseEntity<Void> deleteMajorCoursePlan(@PathVariable Long id) {
        log.debug("REST request to delete MajorCoursePlan : {}", id);
        majorCoursePlanService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
